﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using STT.Business;
using STT.Entities;
using STT.Layer.Connection;
using System.Web;
namespace STT.Admin.UserControls
{
    public partial class ucPhanQuyen : System.Web.UI.UserControl
    {
        ToolBox tb = new ToolBox();
        public bool chonSuaClick;
        protected void Page_Load(object sender, EventArgs e)
        {
           LoadQuyen();
           if (!IsPostBack)
           {
               //tbchucnang.Enabled = true;
           }
        }
        void LoadQuyen()
        {
            gvPhanQuyen.AutoGenerateColumns = false;
            gvPhanQuyen.DataSource = BTbQuyen.SelectAll();
            gvPhanQuyen.DataBind();
        }
       
        void ThongBao(string Loi)
        {
            ScriptManager.RegisterClientScriptBlock(this, GetType(), "Thông báo!", "<script>alert('" + Loi + "')</script>", false);
        }
        protected void btThemMoi_Click(object sender, EventArgs e)
        {
            try
            {

                if (tb.GetDataTableById2("TbQuyen_SelectByTenQuyen", txttenquyen.Text).Rows.Count > 0)
                {
                    ThongBao("Tên quyền đã tồn tại..!");
                }
                else
                {
                    BTbDangNhap.InsertQuyen(txttenquyen.Text);
                    DataTable dt = tb.GetDataTableById2("TbQuyen_SelectByTenQuyen", txttenquyen.Text);
                    for (int i = 1; i < 57; i++)
                    {
                        CheckBox a = FindControl("ql" + i.ToString()) as CheckBox;
                        if (a.Checked == true) BTbDangNhap.InsertChucNangQuyen(dt.Rows[0]["MaQuyen"].ToString(), i.ToString(), "0");
                    }
                    Huy();
                    btThemMoi.Visible = true;
                    btSua.Visible = false;
                    LoadQuyen();
                    Session["action"] = 1;
                }
            }
            catch { }
        }
        protected void lbXuatDuLieu_Click(object sender, EventArgs e)
        {

        }
        private void Huy()
        {
            txttenquyen.Text = "";
            for (int i = 1; i < 57; i++)
            {
                CheckBox a = FindControl("ql" + i.ToString()) as CheckBox;
                a.Checked = false;
            }
        }
        protected void gvPhanQuyen_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "ChonSua")
            {
                // Lay id
                formthemsua.Visible = true;
                lblphanquyen.Text = "Cập nhật quản trị viên";
                txttenquyen.Enabled = false;
                Huy();
                int index = Convert.ToInt32(e.CommandArgument);
                txtmaquyen.Text = gvPhanQuyen.DataKeys[index].Value.ToString();         
                DataTable cnquyen = tb.GetDataTableById("TbDangNhap_GetCNQuyenByMaQuyen", txtmaquyen.Text);
                txttenquyen.Text = cnquyen.Rows[0]["TenQuyen"].ToString();
                for (int i = 0; i < cnquyen.Rows.Count; i++)
                {
                    for (int j = 1; j < 57; j++)
                    {
                        CheckBox a = FindControl("ql" + j.ToString()) as CheckBox;
                        if (cnquyen.Rows[i]["MaCN"].ToString() == j.ToString())
                            a.Checked = true;
                    }
                }
                btSua.Visible = true;
                btThemMoi.Visible = false;

            }
        }
        void LoadDataThem()
        {
      
            txttenquyen.Enabled = true;
            taoquyen.Visible = true;

        }
        protected void btSearch_Click(object sender, ImageClickEventArgs e)
        {
            DataTable dt = new DataTable();
            dt = BTbTDHocVan.SearchTDHocVan(new ETbTDHocVan
            {
                TenTDHocVan = txtTimKiem.Text
            });
            gvPhanQuyen.DataSource = dt;
            gvPhanQuyen.DataBind();
        }
        protected void btSua_Click(object sender, EventArgs e)
        {
            try
            {
                BTbDangNhap.DeleteChucNangQuyen(txtmaquyen.Text);
                for (int i = 1; i < 57; i++)
                {
                    CheckBox a = FindControl("ql" + i.ToString()) as CheckBox;
                    if (a.Checked == true) BTbDangNhap.InsertChucNangQuyen(txtmaquyen.Text, i.ToString(), "1");
                }
                Session["action"] = 2;
            }
            catch (Exception)
            {
                throw;
            }
        }

        protected void lbXoaTat_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < gvPhanQuyen.Rows.Count; i++)
                {
                    CheckBox cbChon = (CheckBox)gvPhanQuyen.Rows[i].Cells[0].FindControl("cbChon");
                    if (cbChon != null && cbChon.Checked)
                    {
                        BTbTDHocVan.Delete(int.Parse(gvPhanQuyen.DataKeys[i].Value.ToString()));
                    }
                }

                LoadQuyen();
                Session["action"] = 3;
            }
            catch
            {
                ThongBao("Dữ liệu liên quan đến một số TĐ Học vấn vẫn tồn tại, vui lòng kiểm tra lại dữ liệu!");
            }
        }

        protected void btThemExcel_Click(object sender, EventArgs e)
        {
          
           
        }

        protected void gvPhanQuyen_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                txtmaquyen.Text = gvPhanQuyen.DataKeys[e.RowIndex].Value.ToString();
                DataTable quyen = tb.GetDataTableById("TbDangNhap_GetByQuyenTenDN", txtmaquyen.Text);
                if (quyen.Rows.Count == 1)
                {
                    if (txtmaquyen.Text == "1")
                    {
                        ThongBao("Bạn không được xóa quyền quản trị Admin");
                    }
                    else
                    {
                        BTbDangNhap.DeleteChucNangQuyen(txtmaquyen.Text);
                        BTbDangNhap.DeleteByMaQuyen(Convert.ToInt32(txtmaquyen.Text));
                        BTbGiangVien.Delete(quyen.Rows[0]["TenDN"].ToString());
                        BTbQuyen.Delete(Convert.ToInt32(txtmaquyen.Text));
                    }

                }
                else
                {
                    if (txtmaquyen.Text == "1")
                    {
                        ThongBao("Bạn không được xóa quyền quản trị Admin");
                    }
                    else
                    {
                        BTbDangNhap.DeleteChucNangQuyen(txtmaquyen.Text);
                        BTbDangNhap.DeleteByMaQuyen(Convert.ToInt32(txtmaquyen.Text));
                        for (int i = 0; i < quyen.Rows.Count; i++)
                        {
                            BTbGiangVien.Delete(quyen.Rows[i]["TenDN"].ToString());
                        }
                        BTbQuyen.Delete(Convert.ToInt32(txtmaquyen.Text));
                    }
                }
                LoadQuyen();
                Session["action"] = 3;
            }
            catch { };
        }

        protected void gvPhanQuyen_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

            gvPhanQuyen.PageIndex = e.NewPageIndex;
            gvPhanQuyen.DataBind();
            
        }
        protected void gvPhanQuyen_PageIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btngoithemmoi_Click(object sender, EventArgs e)
        {
            LoadDataThem();
            lblphanquyen.Text = "Thêm mới quản trị viên";
            formthemsua.Visible = true;
            btThemMoi.Visible = true;
            btSua.Visible = false;
            Huy();
        }

        protected void btnxoa_Click(object sender, EventArgs e)
        {
           
        }
        private void HuyCheckbox()
        {
           
        }
        protected void cktaomoiquyen_CheckedChanged(object sender, EventArgs e)
        {

        }

        protected void Ddlchonquyen_SelectedIndexChanged(object sender, EventArgs e)
        {
          
        }

        protected void bthuy_Click(object sender, EventArgs e)
        {
            Huy();
            formthemsua.Visible = false;
        }

        protected void imgbtmokhoa_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                ImageButton btna = sender as ImageButton;
                GridViewRow row = (GridViewRow)btna.NamingContainer;
                string id = gvPhanQuyen.DataKeys[row.RowIndex].Value.ToString();
                BTbQuyen.UpdateTrangThai(id, "0");
                LoadQuyen();
                Session["action"] = 2;
            }
            catch (Exception ex)
            {
                Response.Write("");
            }
        }

        protected void imgbtkhoa_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                ImageButton btna = sender as ImageButton;
                GridViewRow row = (GridViewRow)btna.NamingContainer;
                string id = gvPhanQuyen.DataKeys[row.RowIndex].Value.ToString();
                if (id == "1")
                {
                    ThongBao("Bạn không được khóa quyền quản trị");
                }
                else
                {
                    BTbQuyen.UpdateTrangThai(id, "1");
                    LoadQuyen();
                    Session["action"] = 2;
                }
            }
            catch (Exception ex)
            {
                Response.Write("");
            }
        }

        protected void gvPhanQuyen_DataBound(object sender, EventArgs e)
        {

        }

        protected void gvPhanQuyen_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton huy = (ImageButton)e.Row.FindControl("imgbtkhoa");
                ImageButton Accept = (ImageButton)e.Row.FindControl("imgbtmokhoa");
                Label lblstatusid = (Label)e.Row.FindControl("lblstatusblock");
                if (lblstatusid.Text == "1")
                {
                    huy.Width = 20;
                    huy.Height = 20;
                    huy.BorderWidth = 1;
                    huy.BorderColor = System.Drawing.Color.Gray;
                }
                else if (lblstatusid.Text == "0")
                {
                    Accept.Height = 20;
                    Accept.Width = 20;
                    Accept.BorderWidth = 1;
                    Accept.BorderColor = System.Drawing.Color.Green;
                }
            }
        }
    }
}